<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="utf-8" />
    
  <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1" />
  <title>
    XDU Python OJ刷题总结 |  GtJason1998
  </title>
  <meta name="generator" content="hexo-theme-yilia-plus">
  
  <link rel="shortcut icon" href="/avatar.jpg" />
  
  
<link rel="stylesheet" href="/css/main.css">

  
<script src="/js/pace.min.js"></script>


  

  

<link rel="alternate" href="/atom.xml" title="GtJason1998" type="application/atom+xml">
</head>

</html>

<body>
  <div id="app">
    <main class="content">
      <section class="outer">
  <article id="post-XDU-Python-OJ刷题总结" class="article article-type-post" itemscope
  itemprop="blogPost" data-scroll-reveal>

  <div class="article-inner">
    
    <header class="article-header">
       
<h1 class="article-title sea-center" style="border-left:0" itemprop="name">
  XDU Python OJ刷题总结
</h1>
  

    </header>
    

    
    <div class="article-meta">
      <a href="/2020/12/10/XDU-Python-OJ%E5%88%B7%E9%A2%98%E6%80%BB%E7%BB%93/" class="article-date">
  <time datetime="2020-12-09T16:24:27.000Z" itemprop="datePublished">2020-12-10</time>
</a>
      
  <div class="article-category">
    <a class="article-category-link" href="/categories/%E6%9D%82%E8%AE%B0/">杂记</a>
  </div>

      
      
<div class="word_count">
    <span class="post-time">
        <span class="post-meta-item-icon">
            <i class="ri-quill-pen-line"></i>
            <span class="post-meta-item-text"> 字数统计:</span>
            <span class="post-count">1.2k字</span>
        </span>
    </span>

    <span class="post-time">
        &nbsp; | &nbsp;
        <span class="post-meta-item-icon">
            <i class="ri-book-open-line"></i>
            <span class="post-meta-item-text"> 阅读时长≈</span>
            <span class="post-count">4分钟</span>
        </span>
    </span>
</div>

      
    </div>
    

    
    
    <div class="tocbot"></div>





    

    
    <div class="article-entry" itemprop="articleBody">
      


      

      
      <h1 id="XDU-Python-OJ刷题总结"><a href="#XDU-Python-OJ刷题总结" class="headerlink" title="XDU Python OJ刷题总结"></a>XDU Python OJ刷题总结</h1><p>这学期选了李隐峰老师的<strong>Python网络数据处理</strong>这门课，虽说到目前为止这门课绝大部分时间都在讲解Python语言的一些基础语法，但是由于这门课安排了课下OJ系统刷题以及上周日(12.6日)刚考完的线上OJ系统机考，所以还是有一定收获的。<br><a id="more"></a></p>
<blockquote>
<p>刷题地址：<a href="https://python.xidian.edu.cn/" target="_blank" rel="noopener">XDU Python OJ</a><br>(貌似只有选了这门课的学生才能使用这个OJ系统，没有对外开放注册入口)</p>
</blockquote>
<ul>
<li><p>关于本地程序的简易调试方法<br>OJ题目的程序写完后测试数据时，每次都从网页复制数据再粘贴到运行终端多少有些麻烦，而且可能有时候还不方便随时添加自己想要测试的数据，同时对输出的数据进行查阅和调试也不太方便<br>所以参考别人的博客(<a href="https://blog.csdn.net/yanxiangtianji/article/details/8826803" target="_blank" rel="noopener">OJ程序简便调试方法</a>)捣鼓了下本地调试的简易方法：</p>
<ul>
<li><p>测试算法程序的文件夹下文件如下：<br><img src="https://gitee.com/gtjason1998/article_images/raw/master/article_images2.png" alt=""></p>
</li>
<li><p><code>1118.py</code>为需要测试的Python算法程序,<code>input.txt</code>中提前写入测试数据,<code>output.txt</code>是用来存放输出结果的文件</p>
</li>
<li><p><code>init.bat</code>脚本文件的内容如下:</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">1118.py&lt;input.txt &gt; output.txt</span><br><span class="line"><span class="built_in">type</span> output.txt</span><br></pre></td></tr></table></figure>
<p><code>1118.py&lt;input.txt</code>部分表示运行1118.py并重定向输入流,将input.txt的内容作为运行1118.py时的输入，后半部分的<code>&gt; output.txt</code>表示将程序的输出结果重定向输出至<code>output.txt</code>文件中<br><code>type output.txt</code>表示将output.txt的内容输出至运行终端，Windows系统平台的<code>type</code>命令类似Linux系统终端下的<code>cat</code>命令</p>
</li>
<li><p>此时写好程序并在input.txt中写好需要测试的输入数据后，直接在对应路径下运行init.bat脚本即可</p>
</li>
</ul>
</li>
<li><p>一道模拟考试时卡住的题目<br>其实挺简单的一道题，当时脑抽了转不过弯来(<del>其实主要还是因为我人菜</del>)，题目如下</p>
<blockquote>
<ul>
<li>题目描述<br>输入一个嵌套列表，嵌套层次不限，根据层次数来求列表元素的加权个数和。<br>第一层每个元素算一个元素，第二层每个元素算2个元素，第三层每个元素算3个元素，第四层每个元素算4个元素,…,以此类推进行计算。</li>
<li>输入<br>在一行中输入一个列表。</li>
<li>输出<br>在一行中输出加权元素个数值。</li>
<li>样例输入<br><strong><code>[1,2,[3,4,[5,6],7],8]</code></strong></li>
<li>样例输出<br><strong><code>15</code></strong></li>
</ul>
</blockquote>
<ul>
<li>正确解题思路✔<br>通过递归的方法，判断当层有没有列表，存在列表就统计该层非列表元素并乘以深度权值同时向内层列表递归并将深度权值加1，不存在列表时直接用该层元素个数乘以权值返回即可，注意递归需设置终止条件</li>
<li>错误解题思路❌<br>模拟考时脑抽了想着用字符串操作来做，先将输入字符串中的<code>,</code>替换为空字符，然后每次遇到<code>[</code>就将权值加1，遇到<code>]</code>就将权值减1，忽略了替换掉<code>,</code>后，元素<code>12</code>将被判定为<code>1</code>和<code>2</code>两个元素，进而计算出错误的结果</li>
<li>程序</li>
</ul>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">def</span> <span class="title">fn</span><span class="params">(base,depth)</span>:</span></span><br><span class="line"><span class="keyword">if</span>(<span class="keyword">not</span> ([i <span class="keyword">for</span> i <span class="keyword">in</span> base <span class="keyword">if</span> type(i) == list])):</span><br><span class="line">    <span class="keyword">return</span> len([i <span class="keyword">for</span> i <span class="keyword">in</span> base])*depth</span><br><span class="line"><span class="keyword">else</span>:</span><br><span class="line">    temp = [i <span class="keyword">for</span> i <span class="keyword">in</span> base <span class="keyword">if</span> type(i) == list]</span><br><span class="line">    tempAmount = <span class="number">0</span></span><br><span class="line">    <span class="keyword">for</span> i <span class="keyword">in</span> temp:</span><br><span class="line">        tempAmount += fn(i,depth+<span class="number">1</span>)</span><br><span class="line">    <span class="keyword">return</span> len([i <span class="keyword">for</span> i <span class="keyword">in</span> base <span class="keyword">if</span> type(i) == int])*depth + tempAmount</span><br><span class="line">List = eval(input())</span><br><span class="line">print(fn(List,<span class="number">1</span>))</span><br></pre></td></tr></table></figure>
</li>
<li><p>心态建设方面<br>其实感觉自己一直就是一个心态不是特别好的人，尤其是OJ系统里这种考试，看着大家切题的提交记录并不停在实时排行榜上刷新浮动时，自己多少会有些紧张，如果自己恰好在某题卡住，那可能基本凉凉。<br><strong>其实人生是场马拉松，不是那么一场两场考试决定的，多和自己比较，总和别人比较很有可能会搞坏自己的心态</strong>，只要自己能够不停的进步就很好，不断进步带来的正反馈是很实时的，害怕犯错害怕失败其实也是玻璃心的一种表现，其实我们的人生有一定的容错性，且我们需要努力去不断地给我们的试错成本加筹码，提高我们人生的容错性。<br>最后的最后，我自己也不过就一个普通人，不会的题不要自己死磕，厉害的人很多，学会看看别人的解题思路，模仿是学习的第一步。</p>
</li>
</ul>

      
      <!-- reward -->
      
    </div>
    
    
      <!-- copyright -->
      
    <footer class="article-footer">
      
      
  <ul class="article-tag-list" itemprop="keywords"><li class="article-tag-list-item"><a class="article-tag-list-link" href="/tags/Leetcode/" rel="tag">Leetcode</a></li><li class="article-tag-list-item"><a class="article-tag-list-link" href="/tags/Python/" rel="tag">Python</a></li></ul>


    </footer>

  </div>

  
  
  <nav class="article-nav">
    
    
      <a href="/2020/05/26/%E8%BD%AC%E8%A1%8C%E7%A8%8B%E5%BA%8F%E5%91%98-%E8%81%8C%E4%B8%9A%E7%94%9F%E6%B6%AF%E8%A7%84%E5%88%92/" class="article-nav-link">
        <strong class="article-nav-caption">下一篇</strong>
        <div class="article-nav-title">职业生涯思考</div>
      </a>
    
  </nav>


  

  
  
<!-- valine评论 -->
<div id="vcomments-box">
    <div id="vcomments">
    </div>
</div>
<script src="//cdn1.lncld.net/static/js/3.0.4/av-min.js"></script>
<script src='https://cdn.jsdelivr.net/npm/valine@1.3.10/dist/Valine.min.js'></script>
<script>
    new Valine({
        el: '#vcomments',
        app_id: '',
        app_key: '',
        path: window.location.pathname,
        notify: 'false',
        verify: 'false',
        avatar: 'mp',
        placeholder: '给我的文章加点评论吧~',
        recordIP: true
    });
    const infoEle = document.querySelector('#vcomments .info');
    if (infoEle && infoEle.childNodes && infoEle.childNodes.length > 0) {
        infoEle.childNodes.forEach(function (item) {
            item.parentNode.removeChild(item);
        });
    }
</script>
<style>
    #vcomments-box {
        padding: 5px 30px;
    }

    @media screen and (max-width: 800px) {
        #vcomments-box {
            padding: 5px 0px;
        }
    }

    #vcomments-box #vcomments {
        background-color: #fff;
    }

    .v .vlist .vcard .vh {
        padding-right: 20px;
    }

    .v .vlist .vcard {
        padding-left: 10px;
    }
</style>

  

  
  
  

</article>
</section>
      <footer class="footer">
  <div class="outer">
    <ul class="list-inline">
      <li>
        &copy;
        2019-2020
        GtJason1998
      </li>
      <li>
        
        Powered by
        
        
        <a href="https://hexo.io" target="_blank">Hexo</a> Theme <a href="https://github.com/Shen-Yu/hexo-theme-ayer" target="_blank">Ayer</a>
        
      </li>
    </ul>
    <ul class="list-inline">
      <li>
        
        
        <span>
  <i>PV:<span id="busuanzi_value_page_pv"></span></i>
  <i>UV:<span id="busuanzi_value_site_uv"></span></i>
</span>
        
      </li>
      
      <li>
        <!-- cnzz统计 -->
        
        <script type="text/javascript" src='https://s9.cnzz.com/z_stat.php?id=1278069914&amp;web_id=1278069914'></script>
        
      </li>
    </ul>
  </div>
</footer>
      <div class="to_top">
        <div class="totop" id="totop">
  <i class="ri-arrow-up-line"></i>
</div>
      </div>
    </main>
    <aside class="sidebar">
      <button class="navbar-toggle"></button>
<nav class="navbar">
  
  <div class="logo">
    <a href="/"><img src="/images/iconDiy-side.png" alt="GtJason1998"></a>
  </div>
  
  <ul class="nav nav-main">
    
    <li class="nav-item">
      <a class="nav-item-link" href="/">主页</a>
    </li>
    
    <li class="nav-item">
      <a class="nav-item-link" href="/categories">分类</a>
    </li>
    
    <li class="nav-item">
      <a class="nav-item-link" href="/tags">标签</a>
    </li>
    
    <li class="nav-item">
      <a class="nav-item-link" href="/archives">归档</a>
    </li>
    
  </ul>
</nav>
<nav class="navbar navbar-bottom">
  <ul class="nav">
    <li class="nav-item">
      
      <a class="nav-item-link nav-item-search"  title="Search">
        <i class="ri-search-line"></i>
      </a>
      
      
    </li>
  </ul>
</nav>
<div class="search-form-wrap">
  <div class="local-search local-search-plugin">
  <input type="search" id="local-search-input" class="local-search-input" placeholder="Search...">
  <div id="local-search-result" class="local-search-result"></div>
</div>
</div>
    </aside>
    <div id="mask"></div>

<!-- #reward -->
<div id="reward">
  <span class="close"><i class="ri-close-line"></i></span>
  <p class="reward-p"><i class="ri-cup-line"></i>请我喝杯咖啡吧~</p>
  <div class="reward-box">
    
    <div class="reward-item">
      <img class="reward-img" src="/images/alipay.jpg">
      <span class="reward-type">支付宝</span>
    </div>
    
    
    <div class="reward-item">
      <img class="reward-img" src="/images/wechat.jpg">
      <span class="reward-type">微信</span>
    </div>
    
  </div>
</div>
    
<script src="/js/jquery-2.0.3.min.js"></script>


<script src="/js/jquery.justifiedGallery.min.js"></script>


<script src="/js/lazyload.min.js"></script>


<script src="/js/busuanzi-2.3.pure.min.js"></script>


<script src="/js/share.js"></script>



<script src="/fancybox/jquery.fancybox.min.js"></script>




<script>
  try {
    var typed = new Typed("#subtitle", {
    strings: ['写是为了更好的思考','',''],
    startDelay: 0,
    typeSpeed: 200,
    loop: true,
    backSpeed: 100,
    showCursor: true
    });
  } catch (err) {
  }
  
</script>




<script src="/js/tocbot.min.js"></script>

<script>
  // Tocbot_v4.7.0  http://tscanlin.github.io/tocbot/
  tocbot.init({
    tocSelector: '.tocbot',
    contentSelector: '.article-entry',
    headingSelector: 'h1, h2, h3, h4, h5, h6',
    hasInnerContainers: true,
    scrollSmooth: true,
    scrollContainer:'main',
    positionFixedSelector: '.tocbot',
    positionFixedClass: 'is-position-fixed',
    fixedSidebarOffset: 'auto',
    onClick: (e) => {
      $('.toc-link').removeClass('is-active-link');
      $(`a[href=${e.target.hash}]`).addClass('is-active-link');
      $(e.target.hash).scrollIntoView();
      return false;
    }
  });
</script>


<script>
  var ayerConfig = {
    mathjax: true
  }
</script>


<script src="/js/ayer.js"></script>


<script src="https://cdn.jsdelivr.net/npm/jquery-modal@0.9.2/jquery.modal.min.js"></script>
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/jquery-modal@0.9.2/jquery.modal.min.css">


<!-- Root element of PhotoSwipe. Must have class pswp. -->
<div class="pswp" tabindex="-1" role="dialog" aria-hidden="true">

    <!-- Background of PhotoSwipe. 
         It's a separate element as animating opacity is faster than rgba(). -->
    <div class="pswp__bg"></div>

    <!-- Slides wrapper with overflow:hidden. -->
    <div class="pswp__scroll-wrap">

        <!-- Container that holds slides. 
            PhotoSwipe keeps only 3 of them in the DOM to save memory.
            Don't modify these 3 pswp__item elements, data is added later on. -->
        <div class="pswp__container">
            <div class="pswp__item"></div>
            <div class="pswp__item"></div>
            <div class="pswp__item"></div>
        </div>

        <!-- Default (PhotoSwipeUI_Default) interface on top of sliding area. Can be changed. -->
        <div class="pswp__ui pswp__ui--hidden">

            <div class="pswp__top-bar">

                <!--  Controls are self-explanatory. Order can be changed. -->

                <div class="pswp__counter"></div>

                <button class="pswp__button pswp__button--close" title="Close (Esc)"></button>

                <button class="pswp__button pswp__button--share" style="display:none" title="Share"></button>

                <button class="pswp__button pswp__button--fs" title="Toggle fullscreen"></button>

                <button class="pswp__button pswp__button--zoom" title="Zoom in/out"></button>

                <!-- Preloader demo http://codepen.io/dimsemenov/pen/yyBWoR -->
                <!-- element will get class pswp__preloader--active when preloader is running -->
                <div class="pswp__preloader">
                    <div class="pswp__preloader__icn">
                        <div class="pswp__preloader__cut">
                            <div class="pswp__preloader__donut"></div>
                        </div>
                    </div>
                </div>
            </div>

            <div class="pswp__share-modal pswp__share-modal--hidden pswp__single-tap">
                <div class="pswp__share-tooltip"></div>
            </div>

            <button class="pswp__button pswp__button--arrow--left" title="Previous (arrow left)">
            </button>

            <button class="pswp__button pswp__button--arrow--right" title="Next (arrow right)">
            </button>

            <div class="pswp__caption">
                <div class="pswp__caption__center"></div>
            </div>

        </div>

    </div>

</div>

<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/photoswipe@4.1.3/dist/photoswipe.min.css">
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/photoswipe@4.1.3/dist/default-skin/default-skin.css">
<script src="https://cdn.jsdelivr.net/npm/photoswipe@4.1.3/dist/photoswipe.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/photoswipe@4.1.3/dist/photoswipe-ui-default.min.js"></script>

<script>
    function viewer_init() {
        let pswpElement = document.querySelectorAll('.pswp')[0];
        let $imgArr = document.querySelectorAll(('.article-entry img:not(.reward-img)'))

        $imgArr.forEach(($em, i) => {
            $em.onclick = () => {
                // slider展开状态
                // todo: 这样不好，后面改成状态
                if (document.querySelector('.left-col.show')) return
                let items = []
                $imgArr.forEach(($em2, i2) => {
                    let img = $em2.getAttribute('data-idx', i2)
                    let src = $em2.getAttribute('data-target') || $em2.getAttribute('src')
                    let title = $em2.getAttribute('alt')
                    // 获得原图尺寸
                    const image = new Image()
                    image.src = src
                    items.push({
                        src: src,
                        w: image.width || $em2.width,
                        h: image.height || $em2.height,
                        title: title
                    })
                })
                var gallery = new PhotoSwipe(pswpElement, PhotoSwipeUI_Default, items, {
                    index: parseInt(i)
                });
                gallery.init()
            }
        })
    }
    viewer_init()
</script>



<script type="text/x-mathjax-config">
  MathJax.Hub.Config({
      tex2jax: {
          inlineMath: [ ['$','$'], ["\\(","\\)"]  ],
          processEscapes: true,
          skipTags: ['script', 'noscript', 'style', 'textarea', 'pre', 'code']
      }
  });

  MathJax.Hub.Queue(function() {
      var all = MathJax.Hub.getAllJax(), i;
      for(i=0; i < all.length; i += 1) {
          all[i].SourceElement().parentNode.className += ' has-jax';
      }
  });
</script>

<script src="https://cdn.jsdelivr.net/npm/mathjax@2.7.6/unpacked/MathJax.js?config=TeX-AMS-MML_HTMLorMML"></script>


<script type="text/javascript" src="https://js.users.51.la/20544303.js"></script>

    
  </div>
<script type="text/x-mathjax-config">
    MathJax.Hub.Config({
        tex2jax: {
            inlineMath: [ ["$","$"], ["\\(","\\)"] ],
            skipTags: ['script', 'noscript', 'style', 'textarea', 'pre', 'code'],
            processEscapes: true
        }
    });
    MathJax.Hub.Queue(function() {
        var all = MathJax.Hub.getAllJax();
        for (var i = 0; i < all.length; ++i)
            all[i].SourceElement().parentNode.className += ' has-jax';
    });
</script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.1/MathJax.js?config=TeX-MML-AM_CHTML"></script>
<!-- script src="http://cdn.mathjax.org/mathjax/latest/MathJax.js?config=TeX-AMS-MML_HTMLorMML"></script>
</body>

</html>